package com.baidu.lib_common.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.baidu.lib_leancloud.db.MusicEntity
import java.lang.reflect.ParameterizedType

abstract class BaseRecyclerViewAdapter<T: ViewBinding,E>
    : RecyclerView.Adapter<BaseRecyclerViewAdapter.MyViewHolder<T>>() {
        private var dataList = listOf<E>()
        fun getDataList(): List<E>{
            return dataList
        }

        private fun createBindingInstance(view: View): T {
            val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
            val clazz = type as Class<*>
            val layoutInflater = LayoutInflater.from(view.context)
            val inflatedView = clazz.getMethod(
                "inflate",
                LayoutInflater::class.java,
                ViewGroup::class.java,
                Boolean::class.java
            ).invoke(null, layoutInflater, view, false) as T

            return inflatedView
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): MyViewHolder<T> {
            return MyViewHolder(createBindingInstance(parent))
        }

        override fun onBindViewHolder(
            holder: MyViewHolder<T>,
            position: Int
        ) {
            bindData(holder.binding, dataList[position])
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        //需要子类去实现
        abstract fun bindData(binding: T, data: E)

        //给外部设置数据源
        fun setNewData(newData: List<E>) {
            dataList = newData
            //val result = DiffUtil.calculateDiff(MyDiffCallbackImpl<E>(dataList, newData))
            //result.dispatchUpdatesTo(this)
            notifyDataSetChanged()
        }
    class MyViewHolder<T : ViewBinding>(val binding: T) : RecyclerView.ViewHolder(binding.root)
}